package org.misty.redis.autoconfigure.locker;

import lombok.extern.slf4j.Slf4j;
import org.misty.redis.autoconfigure.client.RedisClientAutoConfiguration;
import org.misty.redis.client.RedisClient;
import org.misty.redis.locker.RedisLocker;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Misty on 2020-06-10
 */
@Slf4j
@Configuration(proxyBeanMethods = false)
@ConditionalOnExpression("${redis-tools.locker.default.enabled:true}")
@ConditionalOnMissingBean(RedisLocker.class)
@EnableConfigurationProperties(DefaultLockerProperties.class)
@AutoConfigureAfter(RedisClientAutoConfiguration.class)
public class DefaultLockerAutoConfiguration {
    @Bean
    public RedisLocker redisLocker(RedisClient redisClient, DefaultLockerProperties properties) {
        log.info("RedisLocker defaults active! Set 'redis-tools.locker.default.enabled' to 'false' to disable");
        return new RedisLocker(redisClient, properties);
    }
}
